package org.vgou.action.manage;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.vgou.model.Category;
import org.vgou.page.Page;
import org.vgou.page.PageUntils;
import org.vgou.service.ICategoryService;
import org.vgou.util.StrutsUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 
 * Copyright (c) 2011, vgou <br>
 * All rights reserved.<br>
 * 
 * @since 2011-11-30
 * @author <a href="mailto:jason.hxx@gmail.com">jason</a>
 * @version Id: CategoryManageAction.java
 * @comment 商品类型操作类
 * 
 */
@Component("categoryManageAction")
@Scope("prototype")
public class CategoryManageAction  extends ActionSupport implements ModelDriven<Category>{

	private static final long serialVersionUID = -6858912631008269492L;

	protected final Log logger = LogFactory.getLog(getClass());
	
	private int page;
	private int result;
	private long categoryPK = 0;
	private String searchName;
	private Category category = new Category();
	private List<Category> categoryList;
	@Resource
	private ICategoryService categoryService;
	
	/**
	 * 初始化商品类型添加或修改表单
	 * @return
	 * @throws Exception
	 */
	public String initForm() throws Exception{
		if(categoryPK > 0){
			try {
				category = categoryService.getById(categoryPK);
			} catch (Exception e) {
				logger.error("取得商品类型信息失败:" + e.getMessage());
			}
		}
		return "initForm";
	}
	
	/**
	 * 添加或修改商品类型信息
	 * @return
	 * @throws Exception
	 */
	public String addOrUpdate() throws Exception{
		try {
			if(category.getId() <= 0){
				long id = categoryService.create(category);
				if(id > 0)
					result = 1;
				else
					result = -1;
			}else{
				result = categoryService.update(category);
			}
		} catch (Exception e) {
			logger.error("更新商品类型信息失败:" + e.getMessage());
		}
		return "result";
	}
	
	/**
	 * 取得商品类型列表
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception{
		try {
			int perPage = 8;//每页的显示记录数
			int total = categoryService.getAllCount(searchName);
			String params = null;
			if(searchName != null){
				params = "businessName=" + searchName;
			}
			Page pageModel = PageUntils.getPageBar(StrutsUtils.getRequest(), page, perPage , total , params, "page");
			categoryList = categoryService.getAll(searchName, null, false, pageModel.getStartRs(), pageModel.getPerPage());
		} catch (Exception e) {
			logger.error("取得商品类型列表失败:" + e.getMessage());
		}
		return "list";
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

	public long getCategoryPK() {
		return categoryPK;
	}

	public void setCategoryPK(long categoryPK) {
		this.categoryPK = categoryPK;
	}

	public String getSearchName() {
		return searchName;
	}

	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}

	@Override
	public Category getModel() {
		return category;
	}

}
